package CutLoad.components.container
{
	import CutLoad.Base;
	import CutLoad.cookies.BitmapDataTool;
	import CutLoad.cookies.Painter;
	
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	/**
	 * 通过视窗框，查看一个对象 ,与ContainerView不同在于，会把查看切割成网格，只显示要查看的一部份
	 * @author smallerbird mail:smallerbird@gmail.com  site:www.mkigm.com QQ群:49983196
	 * 
	 */	
	public class ContainerViewTiles extends Base
	{
		private var cv:ContainerView;
		private var viewRect:Rectangle;
		private var tileW:Number,tileH:Number;
		private var arrTiles:Array;
		private var row:Number,column:Number;
		
		private var bdTable:ContainerH;
		
		public function ContainerViewTiles(db:BitmapData,row:Number,column:Number,viewRect:Rectangle,speed:Number=2)
		{
			this.row=row;
			this.column=column;
			this.viewRect=viewRect;
			tileW=Math.floor(db.width/column);
			tileH=Math.floor(db.height/row);
			
			bdTable=new ContainerH();
			bdTable.init({w:db.width,isAuto:true,xspace:-1,yspace:-1})
			var bd:BitmapData=db;
			//
			var arrTileBase:Array=BitmapDataTool.cutBitmapDataToTile(bd,row,column);
			arrTiles=new Array();
			for(var i:Number=0;i<arrTileBase.length;i++){
				var arrTilesRow:Array=new Array();
				for(var j:Number=0;j<arrTileBase[i].length;j++){
					var bdTem:BitmapData=arrTileBase[i][j];
					var baseImg:Base=Painter.drawImg(bdTem);
					bdTable.append(baseImg);
					arrTilesRow.push(baseImg);
				}
				arrTiles.push(arrTilesRow);
			}
			bdTable.setTakeWH();	
			//
			cv=new ContainerView(bdTable,viewRect,speed);
			cv.setFunChangPosition(refreshBody);
			//刚开始刷新
			refreshBody(cv)
			//
			this.addChild(cv);
			//
		}
		/**
		 * 获得显示内容的容器 
		 * @return 
		 * 
		 */		
		public function getBody():ContainerH{
			return bdTable
		}
		/**
		 * 返回视窗目前的坐标
		 * @return 
		 * 
		 */		
		public function getViewPosition():Point{
			return cv.getViewPosition()
		}
		/**
		 * 移动视窗 
		 * @param xx
		 * @param yy
		 * 
		 */		
		public function movie(xx:Number,yy:Number):void{
			cv.move(xx,yy);
		}
		/**
		 * 刷新视窗的显示部份 
		 * @param cv
		 * 
		 */		
		public function refreshBody(cv:ContainerView):void{
			var pTem:Point=cv.getViewPosition();
			var startHang:Number=Math.floor(pTem.y/this.tileH);
			var startLie:Number=Math.floor(pTem.x/this.tileW);
			var endHang:Number=Math.floor((pTem.y+viewRect.height)/this.tileH);
			var endLie:Number=Math.floor((pTem.x+viewRect.width)/this.tileW);
			if(startHang>0) startHang--;
			if(endHang<this.row) endHang++;
			if(startLie>0) startLie--;
			if(endLie<this.column) endLie++;
			//
			for(var i:Number=0;i<arrTiles.length;i++){
				for(var j:Number=0;j<arrTiles[i].length;j++){
					if(i>=startHang&&i<=endHang&&j>=startLie&&j<=endLie){
						arrTiles[i][j].visible=true;
					}else{
						arrTiles[i][j].visible=false;
					}
				}
			}
			//
		}
		/**
		 * 上 
		 * 
		 */		
		public function up():void{
			cv.up();
		}
		/**
		 * 右 
		 * 
		 */		
		public function right():void{
			cv.right();
		}
		/**
		 * 下 
		 * 
		 */		
		public function down():void{
			cv.down();
		}
		/**
		 * 左 
		 * 
		 */		
		public function left():void{
			cv.left();
		}
		/**
		 * 取消或显示遮蔽
		 * @param isOk
		 * 
		 */		
		public function set EableMask(isOk:Boolean):void{
			cv.EableMask=isOk;	
		}
		override public function getShowWidth():Number{
			return cv.getShowWidth();
		}
		override public function getShowHeight():Number{
			return cv.getShowHeight();
		}
	}
}